
global.globalKey = "123"
const jwt = require('../util/jwt')

module.exports = class admin_dao extends require('../sql/house_sql'){
    /**
     * 分页获取
     * 获取房子表总信息
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async gethouseList(req,resp){
        console.log(req.query);
        let query = req.query;
        let pageNum = query.pageNum;
        let currPage = query.currPage;

        let data = await this.gethouseListSql(currPage,pageNum)
        let total = await this.gethouseListTotal()
        resp.send({data,total:total[0].count})
    }

    /**
     * 房屋单元录入
     * @param req
     * @param resp
     */
    static async addunitList(req,resp){
        let body = req.body;
        let id = body.id;
        // let token = body.token
        let communityname = body.communityname;
        let unitname = body.unitname;
        let unitnum = body.unitnum;
        let unitstatus = body.unitstatus;
        let unitcontext = body.unitcontext;
        let buildnum = body.buildnum;
        let storeynum = body.storeynum;
        let homenum = body.homenum;
        let homename = body.homename;
        let homestatus = body.homestatus;
        let homemessage = body.homemessage;
        let homesuggestion = body.homesuggestion;
        let airnum = body.airnum;
        let wifiname = body.wifiname;
        let washnum = body.washnum;
        let smartstatus = body.smartstatus;
        let homedesk = body.homedesk;
        let homechair = body.homechair;
        let homeclothes = body.homeclothes;
        let familystatus = body.familystatus;
        let data = await this.addunitListSql(id,communityname,unitname,unitnum,unitstatus,unitcontext,buildnum,storeynum,
            homenum,homename,homestatus,homemessage,homesuggestion,airnum,wifiname,washnum,smartstatus,homedesk,homechair,homeclothes,familystatus)
        resp.send(data)
    }

    /**
     * 房屋单元编辑
     * @param req
     * @param resp
     */
    static async updateunitList(req,resp){
        let body = req.body;
        let u_id = body.u_id;
        let communityname = body.communityname;
        let unitname = body.unitname;
        let unitnum = body.unitnum;
        let unitstatus = body.unitstatus;
        let unitcontext = body.unitcontext;
        let results = await this.updateunitListSql(u_id,communityname,unitname,unitnum,unitstatus,unitcontext);
        resp.send(results)

    }

    /**
     * 单元删除
     * @param req
     * @param resp
     */
    static async delunitList(req,resp){
        let results = await this.delunitListSql(req.query.id)
        resp.send(results)
    }

    /**
     * id获取信息
     * @param req
     * @param resp
     */
    static async getDetailId(req,resp){
        let query = req.query;
        let id = query.id;
        let data = await this.getDetailIdSql(id)
        resp.send(data)
    }

    /**
     * 用户自己房号编辑
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getHouseData(req,resp){
        console.log(req.query);
        let query = req.query;
        let id = query.id;
        let currPage = query.currPage;
        let pageNum = query.pageNum;
        let data = await this.getHouseDataSql(id,currPage,pageNum);
        let total = await this.getHouseDataTotal(id);
        resp.send({data,total:total[0].count})
    }



    /**
     * 分页获取
     * 模糊查询
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async searchList(req,resp){
        let communityname = req.query.communityname;
        let unitname = req.query.unitname;
        let createDt = req.query.createDt;
        let status = req.query.value;
        let currPage =req.query.currPage;
        let pageNum = req.query.pageNum;
        // console.log(req.query.pageNum)
        let data =await this.searchListSql(communityname,unitname,createDt,status,currPage,pageNum)
        let total=await  this.searchListTotal(communityname,unitname,createDt,status)
        resp.send({data,total:total[0].count})
    }

    /**
     * 更新用户h_id
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async updateUserHid(req,resp){
        let body = req.body;
        let id = body.id;
        let h_id = body.h_id;
        let results = await this.updateUserHidSql(id,h_id);
        resp.send(results)
    }

    /**
     * 房号编辑/更新用户住址
     * @param req
     * @param resp
     */
    static async updatenumList(req,resp){
        let body = req.body;
        let id = body.id;
        let unitname = body.unitname;
        let buildnum = body.buildnum;
        let storeynum = body.storeynum;
        let homenum = body.homenum;
        let homename = body.homename;
        let homestatus = body.homestatus;
        let homemessage = body.homemessage;
        let homesuggestion = body.homesuggestion;
        let airnum = body.airnum;
        let wifiname = body.wifiname;
        let washnum = body.washnum;
        let smartstatus = body.smartstatus;
        let homedesk = body.homedesk;
        let homechair = body.homechair;
        let homeclothes = body.homeclothes;
        let familystatus = body.familystatus;

        let homeaddress = body.homeaddress;
        let h_id = body.h_id


        let results = await this.updatenumListSql(id,unitname,buildnum,storeynum,homenum,homename,homestatus,homemessage,homesuggestion,airnum,wifiname,washnum,smartstatus,homedesk,homechair,homeclothes,familystatus);
        results += await this.updateUserHomeSql(h_id,homeaddress);
        resp.send(results)

    }

    /**
     * 房号查询
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async searchnumList(req,resp){
        let homestatus = req.query.homestatus;
        let unitname = req.query.unitname;
        let buildnum = req.query.buildnum;
        let homenum = req.query.homenum;
        let homename = req.query.homename;
        let currPage =req.query.currPage;
        let pageNum = req.query.pageNum;
        // console.log(req.query.pageNum)
        let data =await this.searchnumListSql(homestatus,unitname,buildnum,homenum,homename,currPage,pageNum)
        let total=await this.searchnumListTotal(homestatus,unitname,buildnum,homenum,homename)
        resp.send({data,total:total[0].count})
    }

    /**
     * 房号批量删除
     * @param req
     * @param resp
     */
    static async delnumList(req,resp){
        let results = await this.delnumListSql(req.query.ids)
        resp.send(results)
    }


    /**
     * 智能设备状态改变
     * @param req
     * @param resp
     */
    static async updatehomeStatus(req,resp){
        let body = req.body;
        let id = body.id;
        let smartstatus = body.smartstatus;
        // let familystatus = body.familystatus
        let results = await this.updatehomeStatusSql(id,smartstatus);
        resp.send(results)
    }

    /**
     * 家居设备状态改变
     * @param req
     * @param resp
     */
    static async updatefamilyStatus(req,resp){
        let body = req.body;
        let id = body.id;
        let familystatus = body.familystatus;
        // let familystatus = body.familystatus
        let results = await this.updatefamilyStatusSql(id,familystatus);
        resp.send(results)
    }


    /**
     * 设备查询
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async searchdeviceList(req,resp){
        let smartstatus = req.query.smartstatus;
        let homenum = req.query.homenum;
        let startDate = req.query.startDate;
        let endDate = req.query.endDate;
        let familystatus = req.query.familystatus;
        let currPage =req.query.currPage;
        let pageNum = req.query.pageNum;

        let data =await this.searchdeviceListSql(smartstatus,homenum,startDate,endDate,familystatus,currPage,pageNum)
        let total=await this.searchdeviceListTotal(smartstatus,homenum,startDate,endDate,familystatus)
        resp.send({data,total:total[0].count})
    }

}